package com.team.bus.module.file.controller;

import com.team.bus.module.dd.model.Dd;
import com.team.bus.module.file.model.File;
import com.team.bus.module.file.model.FileRecord;
import com.team.bus.module.file.pagemodel.PFiler;
import com.team.bus.module.file.service.IFileService;
import com.team.bus.module.util.ComboBox;
import com.team.bus.module.util.Excelexport;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.zjframework.traceframe.common.controller.BaseController;
import org.zjframework.traceframe.common.dao.util.StringUtil;
import org.zjframework.traceframe.common.model.DataGrid;
import org.zjframework.traceframe.common.util.ContextHolderUtils;
import org.zjframework.traceframe.common.util.JSONUtil;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * Created by John on 2018/3/28.
 */
@Controller
@RequestMapping("/file")
public class FileController extends BaseController {

    @Autowired
    private IFileService fileService;

    @RequestMapping("/file")
    public String show(){
        return "/file/filelist";
    }
    @RequestMapping("/filerecord")
    public String showrecord(){
        return "/file/filerecordlist";
    }
    @RequestMapping("/addTo")
    public String addto(){
        return "/file/fileAdd";
    }
    @RequestMapping("/editTo")
    public String editto(){
        return "/file/fileEdit";
    }
    @RequestMapping("/fileout")
    public String fileout(){
        return "/file/fileOut";
    }
    @RequestMapping("/filein")
    public String filein(){
        return "/file/fileIn";
    }
    @RequestMapping("/add")
    @ResponseBody
    public JSONUtil add(File file){return fileService.add(file);}

    @RequestMapping("/datagrid")
    @ResponseBody
    public DataGrid<PFiler> tree(String name, String number, String reader, String isindefile){return fileService.tree(name,number,reader,isindefile);}

    @RequestMapping("/recorddatagrid")
    @ResponseBody
    public DataGrid<FileRecord> recordtree(String filename, String start, String end){return fileService.recordtree(filename, start, end);}


    @RequestMapping("/del")
    @ResponseBody
    public JSONUtil del(String ids){return fileService.del(ids);}
    @RequestMapping("/jie")
    @ResponseBody
    public JSONUtil jieyue(String id,String borrower,String number,int copies){return fileService.jieyue(id, borrower, number, copies);}
    @RequestMapping("/huan")
    @ResponseBody
    public JSONUtil guihuan(String id,String borrower,String number,int copies){return fileService.guihuan(id, borrower, number, copies);}
    @RequestMapping("/edit")
    @ResponseBody
    public JSONUtil edit(File file){return fileService.edit(file);}
    @RequestMapping("/getCombox")
    @ResponseBody
    public List<ComboBox> getCombox(String id) {
        String hql = "from Dd where pid=:id";
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        List<Dd> dds = fileService.findHql(hql, params);
        List<ComboBox> comboBoxes = new ArrayList<>();
        ComboBox comboBox=null;
        for(Dd dd : dds){
            comboBox =new ComboBox();
            BeanUtils.copyProperties(dd, comboBox);
            comboBoxes.add(comboBox);
        }
        return comboBoxes;
    }
    @RequestMapping("/datagridExcel")
    public void datagridExcel(
            String name,
            String number,
            String reader,
            String isindefile,
            HttpServletResponse response
    ) {
        String hql = "from File where 1=1";
        Map<String,Object> param = new HashMap<>();
        if(StringUtil.isNotEmpty(name)){
            hql += " and name like:name ";
            param.put("name", "%%"+ name + "%%");
        }
        if(StringUtil.isNotEmpty(number)){
            hql += " and number like:number ";
            param.put("number", "%%"+ number + "%%");
        }
        if(StringUtil.isNotEmpty(reader)){
            hql += " and reader like:reader ";
            param.put("reader", "%%"+ reader + "%%");
        }
        if(StringUtil.isNotEmpty(isindefile)){
            if(Integer.parseUnsignedInt(isindefile) == 1)
            {hql += " and isindefile > 0 ";}
            else
            {
                hql += " and isindefile < 1 ";
            }
        }
        hql += " order by number";
        List<File> files = fileService.findHql(hql,param);
        List<PFiler> pFilers = new ArrayList<>();
        for(File f:files)
        {
            PFiler pFiler = new PFiler();
            BeanUtils.copyProperties(f,pFiler);
            if(f.getIsindefile()>0)
            {
                pFiler.setIsindefile_str("在档"+f.getIsindefile()+"份");
            }else
            {
                pFiler.setIsindefile_str("未在档");
            }
            pFilers.add(pFiler);
        }
        Date d = new Date();//Date类型变量 d
        Long time = d.getTime();//Long类型变量
        String path=	ContextHolderUtils.getServletContext().getRealPath("/");
        String templateFileName = path+"excel/mexcel.xls";
        String resultFileName = time+".xls";
        if(pFilers.size()>0){
            new Excelexport().autoexport(
                    templateFileName,
                    resultFileName,
                    ContextHolderUtils.getRequest(),
                    response,pFilers,null,null);
        }
    }



}
